﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;

namespace Unleashed.AutoUpdate
{
    public abstract class BackgroundWorkerEventArgs : EventArgs
    {
        public bool Cancel { get; set; }
        public object Result { get; set; }
    }

    public abstract class BackgroundWorker<T> where T : BackgroundWorkerEventArgs
    {
        private Thread taskThread;

        public virtual void Start(T e)
        {
            taskThread = new Thread(delegate()
                { 
                    RunWorkerAsync(e);
                });
            taskThread.Start();
        }

        public virtual event ProgressChangedEventHandler ProgressChanged;
        public virtual event RunWorkerCompletedEventHandler Completed;

        protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
        {
            if (ProgressChanged != null)
                ProgressChanged(this, e);
        }

        protected virtual void OnCompleted(RunWorkerCompletedEventArgs e)
        {
            if (Completed != null)
                Completed(this, e);
        }

        protected abstract void RunWorkerAsync(T e);
    }
}